package cn.pegasus.teamwork.pegasus.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import cn.iyizhan.teamwork.base.util.PageObject;
import cn.iyizhan.teamwork.base.util.ParameterEunm;
import cn.iyizhan.teamwork.base.util.ReMessage;
import cn.pegasus.teamwork.manager.controller.BaseManagerController;
import cn.pegasus.teamwork.pegasus.entity.BussCouponInfo;
import cn.pegasus.teamwork.pegasus.entity.BussUserCoupon;
import cn.pegasus.teamwork.pegasus.mapper.BussCouponInfoMapper;
import cn.pegasus.teamwork.pegasus.service.BussCouponInfoService;
import cn.pegasus.teamwork.pegasus.service.BussUserCouponService;
import cn.pegasus.teamwork.util.DateCompareUtil;
import cn.pegasus.teamwork.util.OSSClientUtil;

@Controller
@RequestMapping("/coupon")
public class CouponController extends BaseManagerController<BussCouponInfo, BussCouponInfoMapper>{

	private static final Logger logger = LoggerFactory.getLogger(CouponController.class);
	
	@Autowired
	private BussCouponInfoService couponInfoService;
	@Autowired
	private BussUserCouponService userCouponService;
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("coupon/coupon-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			PageObject<BussCouponInfo> dicList = couponInfoService.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询服务列表信息异常",e.getMessage());
		}
		return view;
	}
	
	/**
	 * 添加新增页面
	 */
	 @RequestMapping(value="/goAdd" ,method = {RequestMethod.GET})
	 public ModelAndView goAdd(@RequestParam(value="type",required = false) String type,
			 @RequestParam(value="id",required = false) Long id)throws Exception{
		 if(null == type){
			 ModelAndView view = new ModelAndView("coupon/coupon-add");
			 return view;
		 }else{
			 ModelAndView view = new ModelAndView("coupon/coupon-edit");
			 BussCouponInfo news = couponInfoService.couponInfo(id);
			 view.addObject("info", news);
			 return view;
		 } 
	 }
	 
	 /**
	  * 新增
	  * newsType 1-帮助中心；2-关于我们；3-其他类型,
	  */
	 @ResponseBody
	 @RequestMapping(value = "/addCoupon", method = RequestMethod.POST)
	 public String addCoupon(HttpServletRequest request,
			 @RequestParam(value = "couponName", required = false) String couponName,
			 @RequestParam(value = "endTime", required = false) String endTime,
			 @RequestParam(value = "startTime", required = false) String startTime,
			 @RequestParam(value = "couponPrice", required = false) Double couponPrice,
			 @RequestParam(value = "couponDesc", required = false) String couponDesc,
			 @RequestParam(value = "userCount", required = false) Integer userCount,
			 @RequestParam(value = "couponCount", required = false) Integer couponCount,
			 @RequestParam(value = "ccouponUsetype", required = false) Integer ccouponUsetype,
			 @RequestParam(value = "ccouponUseparam", required = false) String ccouponUseparam){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	BussCouponInfo info = new BussCouponInfo();
        	info.setCouponName(couponName);
        	if(StringUtils.isNotBlank(endTime)){
        		info.setEndTime(DateCompareUtil.getDateToString(endTime));
        	}
        	if(StringUtils.isNotBlank(startTime)){
        		info.setStartTime(DateCompareUtil.getDateToString(startTime));
        	}
        	if(null != couponPrice){
        		long price = (long) (couponPrice*100);
        		info.setCouponPrice(price);
        	}
        	info.setCouponDesc(couponDesc);
        	if(null == userCount){
        		userCount = 0;
        	}
        	info.setUserCount(userCount);
        	info.setCouponCount(couponCount);
        	info.setCcouponUsetype(ccouponUsetype);
        	info.setCcouponUseparam(ccouponUseparam);
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String couponImages = util.uploadImg2Oss(file);
            	info.setCouponImages(couponImages);
        	}
        	couponInfoService.addCoupon(getAdmin().getId(), info);
        } catch (Exception e) {
            logger.error("新增优惠券异常", e);
            return "1001";
        }
        return "200";
	 }
	 
	 /**
	  * 修改
	  */
	 @ResponseBody
	 @RequestMapping(value="/updateCoupon", method = RequestMethod.POST)
	 public String updateCoupon(HttpServletRequest request,
			 @RequestParam(value = "couponName", required = false) String couponName,
			 @RequestParam(value = "endTime", required = false) String endTime,
			 @RequestParam(value = "startTime", required = false) String startTime,
			 @RequestParam(value = "couponPrice", required = false) Double couponPrice,
			 @RequestParam(value = "couponDesc", required = false) String couponDesc,
			 @RequestParam(value = "userCount", required = false) Integer userCount,
			 @RequestParam(value = "couponCount", required = false) Integer couponCount,
			 @RequestParam(value = "ccouponUsetype", required = false) Integer ccouponUsetype,
			 @RequestParam(value = "ccouponUseparam", required = false) String ccouponUseparam,
			 @RequestParam(value = "id", required = false) Long id){
        try {
        	MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
        	MultipartFile file = fileRequest.getFile("file");
        	BussCouponInfo info = new BussCouponInfo();
        	info.setCouponName(couponName);
        	if(StringUtils.isNotBlank(endTime)){
        		info.setEndTime(DateCompareUtil.getDateToString(endTime));
        	}
        	if(StringUtils.isNotBlank(startTime)){
        		info.setStartTime(DateCompareUtil.getDateToString(startTime));
        	}
        	if(null != couponPrice){
        		long price = (long) (couponPrice*100);
        		info.setCouponPrice(price);
        	}
        	info.setCouponDesc(couponDesc);
        	if(null == userCount){
        		userCount = 0;
        	}
        	info.setUserCount(userCount);
        	info.setCouponCount(couponCount);
        	info.setCcouponUsetype(ccouponUsetype);
        	info.setCcouponUseparam(ccouponUseparam);
        	info.setId(id);
        	if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
        		//文件上传
            	OSSClientUtil util = new OSSClientUtil();
            	String couponImages = util.uploadImg2Oss(file);
            	info.setCouponImages(couponImages);
        	}
        	couponInfoService.updateCoupon(getAdmin().getId(), info);
        } catch (Exception e) {
            logger.error("修改优惠券信息异常", e);
            return "1001";
        }
        return "200";
	 }
	 
	 /**
	  * 删除
	  */
	 @ResponseBody
	 @RequestMapping(value="/delete")
	 public String delete(@RequestParam(value="id",required = false) Long id){
        try {
        	couponInfoService.delete(getAdmin().getId(), id);
        } catch (Exception e) {
            logger.error("删除区域异常", e);
            return "1001";
        }
        return "200";
	 }
	 /**
	  * 查看详情
	  */
	 @RequestMapping(value="/couponInfo" ,method = {RequestMethod.GET})
	 public ModelAndView couponInfo(@RequestParam(value="id",required = false) Long id){
			 ModelAndView view = new ModelAndView("coupon/coupon-info");
			 BussCouponInfo news = couponInfoService.couponInfo(id);
			 view.addObject("info", news);
			 return view;
	 }
	 
	 /**
	  * 用户优惠券列表
	  */
	 @RequestMapping(value="/userCouponList")
	public ModelAndView userCouponList(@RequestParam Map<String, Object> map){
		ModelAndView view = new ModelAndView("coupon/user-list");
		if(map.isEmpty()){
			map.put("pageData", 10);
			map.put("curPage", 1);
		}
		try{
			PageObject<BussUserCoupon> dicList = userCouponService.PageList(map);
			view.addObject("list", dicList);
			view.addObject("map", map);
		}catch(Exception e){
			logger.error("查询用户优惠券列表信息异常",e.getMessage());
		}
		return view;
	}
}
